describe('API Integration Tests', () => {
  const baseApiUrl = Cypress.env('BACKEND_URL')
  
  describe('后端API可用性测试', () => {
    it('应该能够连接到后端服务', () => {
      cy.apiRequest('GET', '/actuator/health')
        .then((response) => {
          // Spring Boot健康检查端点
          if (response.status === 200) {
            expect(response.body.status).to.equal('UP')
          } else {
            cy.log(`Health check returned: ${response.status}`)
          }
        })
    })

    it('应该能够访问Swagger API文档', () => {
      cy.request({
        url: `${baseApiUrl}/swagger-ui/index.html`,
        failOnStatusCode: false
      }).then((response) => {
        // Swagger应该可访问
        cy.log(`Swagger UI status: ${response.status}`)
        expect([200, 302, 404]).to.include(response.status)
      })
    })
  })

  describe('SMT Controller API测试', () => {
    it('应该测试订单信息查询', () => {
      cy.fixture('test-data').then((testData) => {
        cy.apiRequest('GET', `/api/smt/order/${testData.orders.valid.orderNo}`)
          .then((response) => {
            cy.log(`Order API response: ${response.status}`)
            
            if (response.status === 200) {
              expect(response.body).to.have.property('code')
              expect(response.body).to.have.property('data')
            } else if (response.status === 404) {
              // 订单不存在也是有效的测试结果
              expect(response.body).to.have.property('code')
            }
          })
      })
    })

    it('应该测试BOM信息查询', () => {
      cy.fixture('test-data').then((testData) => {
        const { orderNo, machineId, surface } = testData.orders.valid
        
        cy.apiRequest('GET', `/api/smt/bom/${orderNo}/${machineId}/${surface}`)
          .then((response) => {
            cy.log(`BOM API response: ${response.status}`)
            
            if (response.status === 200) {
              expect(response.body).to.have.property('code')
              expect(response.body).to.have.property('data')
              
              if (response.body.data) {
                expect(response.body.data).to.be.an('array')
              }
            }
          })
      })
    })

    it('应该测试扫描记录提交', () => {
      cy.fixture('test-data').then((testData) => {
        const scanRecord = {
          orderNo: testData.orders.valid.orderNo,
          mounterId: testData.orders.valid.machineId,
          smtSide: 'TOP',
          partId: testData.parts.valid[0].partId,
          rollNo: testData.parts.valid[0].rollNo,
          lotNo: testData.parts.valid[0].lotNo,
          operatorId: testData.operators.valid.operatorId,
          scanTime: new Date().toISOString()
        }
        
        cy.apiRequest('POST', '/api/smt/scan-record', scanRecord)
          .then((response) => {
            cy.log(`Scan record API response: ${response.status}`)
            
            // 验证API响应格式
            expect(response.body).to.have.property('code')
            
            if (response.status === 200) {
              expect(response.body.code).to.equal('200')
            } else {
              // 记录具体的错误信息
              cy.log(`API Error: ${JSON.stringify(response.body)}`)
            }
          })
      })
    })
  })

  describe('Worker Controller API测试', () => {
    it('应该测试作业员信息查询', () => {
      cy.fixture('test-data').then((testData) => {
        cy.apiRequest('GET', `/api/workers/${testData.operators.valid.operatorId}`)
          .then((response) => {
            cy.log(`Worker API response: ${response.status}`)
            
            if (response.status === 200) {
              expect(response.body).to.have.property('code')
              expect(response.body).to.have.property('data')
              
              if (response.body.data) {
                expect(response.body.data).to.have.property('operatorId')
              }
            }
          })
      })
    })

    it('应该测试无效作业员ID', () => {
      cy.fixture('test-data').then((testData) => {
        cy.apiRequest('GET', `/api/workers/${testData.operators.invalid.operatorId}`)
          .then((response) => {
            cy.log(`Invalid worker API response: ${response.status}`)
            
            // 无效ID应该返回404或错误代码
            expect([404, 400, 500]).to.include(response.status)
          })
      })
    })
  })

  describe('BOM Controller API测试', () => {
    it('应该测试BOM数据查询', () => {
      cy.fixture('test-data').then((testData) => {
        const { orderNo, machineId, surface } = testData.orders.valid
        
        cy.apiRequest('GET', `/api/bom/${orderNo}/${machineId}/${surface}`)
          .then((response) => {
            cy.log(`BOM API response: ${response.status}`)
            
            if (response.status === 200) {
              expect(response.body).to.have.property('code')
              expect(response.body).to.have.property('data')
            } else {
              // BOM不存在也是有效的测试场景
              expect(response.body).to.have.property('code')
            }
          })
      })
    })
  })

  describe('Roll Controller API测试', () => {
    it('应该测试Roll信息查询', () => {
      cy.fixture('test-data').then((testData) => {
        const { orderNo, machineId, surface } = testData.orders.valid
        
        cy.apiRequest('GET', `/api/rolls/${orderNo}/${machineId}/${surface}`)
          .then((response) => {
            cy.log(`Roll API response: ${response.status}`)
            
            if (response.status === 200) {
              expect(response.body).to.have.property('code')
              expect(response.body).to.have.property('data')
            }
          })
      })
    })

    it('应该测试Roll验证', () => {
      cy.fixture('test-data').then((testData) => {
        const rollVerification = {
          orderNo: testData.orders.valid.orderNo,
          mounterId: testData.orders.valid.machineId,
          partId: testData.parts.valid[0].partId,
          rollNo: testData.parts.valid[0].rollNo
        }
        
        cy.apiRequest('POST', '/api/rolls/verify', rollVerification)
          .then((response) => {
            cy.log(`Roll verification response: ${response.status}`)
            expect(response.body).to.have.property('code')
          })
      })
    })
  })

  describe('错误场景集成测试', () => {
    it('应该测试数据库连接异常', () => {
      // 通过访问需要数据库的API来测试连接
      cy.apiRequest('GET', '/api/smt/order/NONEXISTENT')
        .then((response) => {
          cy.log(`Database connection test: ${response.status}`)
          // 如果数据库连接正常，应该返回404或业务错误码
          // 如果数据库连接异常，可能返回500
          expect([200, 404, 500]).to.include(response.status)
        })
    })

    it('应该测试无效API路径', () => {
      cy.apiRequest('GET', '/api/invalid/endpoint')
        .then((response) => {
          // 应该返回404
          expect(response.status).to.equal(404)
        })
    })
  })
})